package com.gjw.com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gjw.com.bean.Person;
import com.gjw.com.bean.Student;
import com.gjw.com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {

    @Autowired
    StudentService studentService;

    @Autowired
    RestTemplate restTemplate;


    @GetMapping("/restTemp")
    public Object restTemp(){
//        RestTemplate restTemplate = new RestTemplate();
        HashMap<Object, Object> params = new HashMap<>();
        params.put("pageNum",2);
        params.put("pageSize",3);

        JSONArray forObject = restTemplate.getForObject("http://192.168.0.44:8082/test?pageNum={pageNum}&pageSize={pageSize}", JSONArray.class,1,5);
        System.out.println(forObject);
        return forObject;
    }

    @GetMapping("/restTemp2")
    public Object restTemp2(){
        RestTemplate restTemplate = new RestTemplate();
//        HashMap<Object, Object> params = new HashMap<>();
//        params.put("pageNum",2);
//        params.put("pageSize",3);
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxf8611197fe61cf64&secret=6fd412aa8b6d5782e57e6b1bc05286f0";
        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
        String access_token = (String) forObject.get("access_token");
        String access_token2 = (String) forObject.get("access_token2");
        System.out.println(forObject);
        System.out.println("access_token:"+access_token);
        System.out.println("access_token2:"+access_token2);
        return forObject;
    }



    @GetMapping("/test")
    public List<Student>  test(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        return studentService.getStudent(pageNum,pageSize);
    }

    @GetMapping("/selectStudentById")
    public Student selectStudentById(@RequestParam("id") String id){
        return  studentService.selectById(id);
    }

    @PostMapping("/test02")
    public Object test02(@RequestBody JSONObject jsonObject){
        Object obj = jsonObject.get("obj");
        HashMap<String, Object> map = new HashMap<>();
//        HashMap hashMap = jsonObject.toJavaObject()
        Map toJavaObject = JSONObject.toJavaObject(jsonObject, Map.class);

        map.put("json",toJavaObject);
        map.put("obj",obj);

        return map;



//        System.out.println(jsonString);
//        JSONObject parse = (JSONObject) JSONObject.parse(jsonString);
//        JSONArray arr = (JSONArray) parse.get("arr");
//        String o = (String)arr.get(0);
//        System.out.println("o:"+o);



//        JSONObject objParse = (JSONObject)parse.get("obj");
////        JSONObject objParse = (JSONObject) JSONObject.parse(objStr);
//        String a = (String) objParse.get("a");
//        Integer b = (Integer) objParse.get("b");
//        Boolean c = (Boolean) objParse.get("c");
//        System.out.println("a:"+a);
//        System.out.println("b:"+b);
//        System.out.println("c:"+c);
//        Person person = JSONObject.parseObject(jsonString, Person.class);
//        JSONObject parse = (JSONObject) JSONObject.parse(jsonString);
//        String name = (String) parse.get("name");
//        System.out.println("name:"+name);


//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("name","test");
//        jsonObject.put("age",123);
//        jsonObject.put("boo",false);
//
//        String s = jsonObject.toJSONString();

//        return arr;
    }
}
